﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;

namespace popGis
{
    /// <summary>
    /// 此类包含一堆静态方法，以实现多种可视化显示
    /// </summary>
    class VisualDisplay
    {
        /// <summary>
        /// 从IGeometry获得IElement显示元素
        /// </summary>
        /// <param name="pGeometry"></param>
        /// <returns></returns>
        public static IElement IGeometry2IElement(IGeometry pGeometry)
        {                
            IElement pElement = null;

            if (pGeometry.GeometryType == esriGeometryType.esriGeometryPoint)
            {
                pElement = getIElement(120, 0, 25, 5, esriSimpleMarkerStyle.esriSMSCircle);
                pElement.Geometry = pGeometry;
            }

            return pElement;
        }

        ///<summary>
        ///添加临时元素到地图窗口上
        ///</summary>
        ///<param name="pMapCtrl">地图控件</param>
        ///<param name="pElement">单个元素</param>
        ///<param name="pElementCollection">元素集合</param>
        public static void AddTempElement(AxMapControl pMapCtrl, IElement pElement, IElementCollection pElementCollection)
        {
            try
            {
                IMap pMap = pMapCtrl.Map;
                IGraphicsContainer pGCs = pMap as IGraphicsContainer;

                if (pElementCollection != null)
                    if (pElementCollection.Count > 0)
                        pGCs.AddElements(pElementCollection, 0);

                if (pElement != null)
                    pGCs.AddElement(pElement, 0);//小技巧：注意一般会把单独的点元素放置在其他类型的元素层之上

                IActiveView pAV = (IActiveView)pMap;
                
                pAV.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, pAV.Extent);//刷新显示
            }
            catch (Exception Err)
            {
                System.Windows.Forms.MessageBox.Show(Err.Message, "提示");
            }
        }

        /// <summary>
        /// 设置一个要素对象的显示方案，使用ISimpleMarkerSymbol
        /// </summary>
        /// <returns></returns>
        public static IElement getIElement(int r, int g, int b, int size, esriSimpleMarkerStyle style)
        {
            IElement pElement = null;

            ISimpleMarkerSymbol pMarkerSymbol = new SimpleMarkerSymbol();
            IRgbColor pRgbColor = new RgbColorClass();
            pRgbColor.Red = r;
            pRgbColor.Green = g;
            pRgbColor.Blue = b;
            pMarkerSymbol.Color = pRgbColor;          //设置颜色
            pMarkerSymbol.Size = size;      //设置大小单位为像素
            pMarkerSymbol.Style = style;              //设置形状
            IMarkerElement pMarkerElement = new MarkerElementClass();
            pMarkerElement.Symbol = (IMarkerSymbol)pMarkerSymbol;

            pElement = (IElement)pMarkerElement;
            return pElement;
        }
    }
}
